package com.ysq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysq.controller.exception.CustomException;
import com.ysq.entity.Category;
import com.ysq.entity.Dish;
import com.ysq.entity.Employee;
import com.ysq.entity.Setmeal;
import com.ysq.mapper.CategoryMapper;
import com.ysq.mapper.DishMapper;
import com.ysq.mapper.SetmealMapper;
import com.ysq.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: CategoryServiceImpl
 * Package: com.ysq.service.impl
 * Description:
 *
 * @Author YSQ
 * @Create 2023/1/3 - 11:00
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper mapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public boolean save(Category category) {
        int row = mapper.insert(category);
        if (row > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Page<Category> selectPage(int page, int pageSize) {
        //构造分页构造器 ==> 创建page分页对象, 设置分页参数
        Page<Category> pageInfo = new Page<>(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        //根据 sort 进行升序排序
        lqw.orderByAsc(Category::getSort);
        //执行查询
        mapper.selectPage(pageInfo, lqw);
        //查询完了之后 pageInfo 中就已经包含了分页相关的全部数据
        return pageInfo;
    }

    @Override
    public boolean delete(Long id) {
        //Dish, Setmeal都有属性categoryId, 这个就是它们对应关联的分类ID
        //由此可以构造条件构造器
        LambdaQueryWrapper<Dish> lqwDish = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Setmeal> lqwSetmeal = new LambdaQueryWrapper<>();

        //查询当前分类是否关联了菜品, 如果已经关联, 就抛出一个业务异常
        //1. 设置查询条件 等价于 where categoryId = id
        lqwDish.eq(Dish::getCategoryId, id);
        //2. 执行查询 select count(*) from dish where categoryId = id
        Long count1 = dishMapper.selectCount(lqwDish);
        //3. 判断
        if (count1 > 0) {
            //这个分类已经关联了菜品, 抛出业务异常
            throw new CustomException("已经关联菜品, 不能删除~~~");
        }

        //查询当前分类是否关联了套餐, 如果已经关联, 就抛出一个业务异常
        //1. 设置查询条件 等价于 where categoryId = id
        lqwSetmeal.eq(Setmeal::getCategoryId, id);
        //2. 执行查询 select count(*) from setmeal where categoryId = id
        Long count2 = setmealMapper.selectCount(lqwSetmeal);
        //3. 判断
        if (count2 > 0) {
            //这个分类已经关联了套餐, 抛出业务异常
            throw new CustomException("已经关联套餐, 不能删除~~~");
        }

        //如果上述都不执行代表这个分类没有关联菜品或者套餐, 可以正常删除了
        int row = mapper.deleteById(id);
        if (row > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean update(Category category) {
        int row = mapper.updateById(category);
        if (row > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<Category> selectByType(Category category) {
        //构造条件构造器
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        //添加条件
        lqw.eq(category.getType() != null, Category::getType, category.getType());//按种类查询
        //添加排序条件
        //根据sort进行升序, 在升序的基础上再根据更新时间也就是updateTime进行降序排序
        lqw.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        //执行查询
        List<Category> categories = mapper.selectList(lqw);
        //返回查询结果
        return categories;
    }
}
